package com.sleepcamel.resteasyConfigurator.view;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.swt.widgets.Tree;

import com.sleepcamel.rest.domain.Mapping;
import com.sleepcamel.rest.domain.Project;
import com.sleepcamel.rest.domain.ProjectPackage;
import com.sleepcamel.rest.domain.ProjectRootItem;
import com.sleepcamel.rest.domain.method.NonRESTMethod;
import com.sleepcamel.rest.domain.method.RESTMethod;
import com.sleepcamel.resteasyConfigurator.components.tree.ProjectTreeViewer;
import com.sleepcamel.resteasyConfigurator.components.tree.strategies.ETreeStrategy;
import com.sleepcamel.resteasyConfigurator.model.App;
import com.sleepcamel.resteasyConfigurator.util.ImageDescriptorProvider;
import com.sleepcamel.resteasyConfigurator.view.detail.DetailView;
import com.sleepcamel.resteasyConfigurator.view.detail.EmptyDetailView;
import com.sleepcamel.resteasyConfigurator.view.detail.ProjectDetailView;

public class MainContainer extends Composite {

	private App app = App.getInstance();
	private ProjectTreeViewer projectTreeViewer;
	
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 * @param shlResteasyConfigurator 
	 */
	public MainContainer(Composite parent, int style) {
		super(parent, style);
		setLayout(new FillLayout(SWT.HORIZONTAL));
		
		SashForm sashForm = new SashForm(this, SWT.NONE);
		
		Composite treeToolbarComposite = new Composite(sashForm, SWT.NONE);
		treeToolbarComposite.setLayout(new GridLayout(1, false));
		
		ToolBar toolBar = new ToolBar(treeToolbarComposite, SWT.FLAT);
		toolBar.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));
		
		final ToolItem packageItem = new ToolItem(toolBar, SWT.RADIO);
		packageItem.setToolTipText("Package view");
		packageItem.setImage(ImageDescriptorProvider.getImageDescriptor("container_object.png").createImage());
		
		final ToolItem publishItem = new ToolItem(toolBar, SWT.RADIO);
		publishItem.setToolTipText("Publish view");
		publishItem.setImage(ImageDescriptorProvider.getImageDescriptor("hand.png").createImage());
		
		packageItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				if ( packageItem.getSelection() ){
					projectTreeViewer.setStrategy(ETreeStrategy.PACKAGE);
					projectTreeViewer.refresh();
				}
			}
		});
		
		publishItem.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				if ( publishItem.getSelection() ){
					projectTreeViewer.setStrategy(ETreeStrategy.PUBLISH);
					projectTreeViewer.refresh();
				}
			}
		});
		
		projectTreeViewer = new ProjectTreeViewer(treeToolbarComposite);
		Tree tree = projectTreeViewer.getTree();
		tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true, 1, 1));
		
		Composite selectedOptionContainer = new Composite(sashForm, SWT.NONE);
		
		projectTreeViewer.addSelectionChangedListener(new TreeSelectionChangedListener(sashForm, selectedOptionContainer));

		packageItem.setSelection(true);
		sashForm.setWeights(new int[] {2, 5});

	}

	public void refreshTree(boolean reset){
		if ( reset ){
			Project currentProject = app.getCurrentProject();
			if ( currentProject != null ){
				ProjectRootItem projectRootItem = new ProjectRootItem();
				projectRootItem.addChild(currentProject);
				projectTreeViewer.setInput(projectRootItem);
				projectTreeViewer.expandAll();
				projectTreeViewer.setSelection(new StructuredSelection(currentProject));
			}
		}else{
		}
	}

}

class TreeSelectionChangedListener implements ISelectionChangedListener {
	private Composite selectedOptionContainer;
	private SashForm sashForm;

	public TreeSelectionChangedListener(SashForm sashForm, Composite selectedOptionContainer) {
		this.sashForm = sashForm;
		this.selectedOptionContainer = selectedOptionContainer;
	}
	
	@SuppressWarnings("unchecked")
	public void selectionChanged(SelectionChangedEvent event) {
		// if the selection is empty clear the label
		IStructuredSelection selection = (IStructuredSelection)event.getSelection();
		if (! event.getSelection().isEmpty()) {
			Object firstElement = selection.getFirstElement();
			
			selectedOptionContainer.dispose();
			@SuppressWarnings("rawtypes")
			DetailView detailViewForData = getDetailViewForData(firstElement);
			detailViewForData.setModelData(firstElement);
			selectedOptionContainer = detailViewForData;
			sashForm.setWeights(new int[] {2, 5});
			sashForm.layout();
		}
	}
	
	private DetailView<?> getDetailViewForData(Object data) {
		if ( data == null )
			return null;
		
		if ( data.getClass().equals(Project.class))
			return new ProjectDetailView(sashForm, SWT.NONE);
		
		if ( data.getClass().equals(ProjectPackage.class))
			return new EmptyDetailView<ProjectPackage>(sashForm, SWT.NONE);
		
		if ( data.getClass().equals(Mapping.class))
			return new MappingDetailSourceCodeComposite(sashForm, SWT.NONE);
		
		if ( data.getClass().equals(RESTMethod.class))
			return new MethodDetailSourceCodeComposite<RESTMethod>(sashForm, SWT.NONE);
		
		if ( data.getClass().equals(NonRESTMethod.class))
			return new MethodDetailSourceCodeComposite<NonRESTMethod>(sashForm, SWT.NONE);
		return null;
	}

}
